﻿using System;
using System.Threading.Tasks;
using Grpc.Core;
using Hello;
using Vno;

namespace GreetServiceServer
{
    class GreetServiceImpl : GreetService.GreetServiceBase
    {
        static int i = 0;
        // Server side handler of the SayHello RPC
        public override Task<HelloReply> SayHello(HelloRequest request, ServerCallContext context)
        {
            Console.WriteLine("GreetService server call SayHello() -> " + "Hello " + request.Name + i++);
            return Task.FromResult(new HelloReply { Message = "Hello " + request.Name });
        }
    }

    class VnoServiceImpl : VnoService.VnoServiceBase
    {
        public override Task<VnoResponse> Run(VnoRequest request, ServerCallContext context)
        {
            Console.WriteLine("VnoService server call Run() -> " + "输入参数: 最大迭代次数 - " + request.MaxNumber);
            return Task.FromResult(new VnoResponse { Success = false });
        }
    }

    class Program
    {
        const string host = "localhost";
        const int Port = 50051;

        public static void Main(string[] args)
        {
            Server server = new Server
            {
                // 绑定2个服务
                Services = { GreetService.BindService(new GreetServiceImpl()), VnoService.BindService(new VnoServiceImpl()) },
                Ports = { new ServerPort(host, Port, ServerCredentials.Insecure) }
            };
            server.Start();

            Console.WriteLine("GreetService server listening on port " + Port);
            Console.WriteLine("Press any key to stop the server...");
            Console.ReadKey();

            server.ShutdownAsync().Wait();
        }
    }
}